<?php

class DefaultController extends Controller {

    public $content = array();

    public function actionIndex() {

        $model = new LoginForm;
        if (isset($_POST['ajax']) && $_POST['ajax'] === 'login-form') {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }

        // collect user input data
        if (isset($_POST['LoginForm'])) {
          
            $model->attributes = $_POST['LoginForm'];
            // validate user input and redirect to the previous page if valid
            if ($model->validate() && $model->login())
                $this->redirect('home/view');
                //echo 'ok';
        }
        // display the login form
        if(!isset(user()->getModel()->username))
          $this->render('index', array('model' => $model));
        else
          $this->redirect('home/view');
        
    }

}
